require(["jquery", "paging", "template", "bootstrap", "echarts", "bbcare", "scprit/department/view/template/follow-up/common.js"], function ($, Paging, template, bootstrap, echarts, $bb, $common) {
	var URL = $bb.baseUrl("department") + "followup/service"
	var obj = $bb.getPageParams()
	var userId = obj.babyId
	var type = obj.type//历史记录传参；
	'use strict'
	var count = 1, pagesize = 15//历史记录分页；
	var HashMap = $common.hashmap(), hashMap = new HashMap()
	$common.date()
	var btnSubmit = $("#submitData")
	getBabyInfo()
	getAllPostion()

	$(".mask, .AddInformation").hide()
	$("#historyData").click(function (event) {
		getBabyList(1, pagesize)
		var p = new Paging()
		$("#pageTool").html("")
		p.init({
			target: '#pageTool', pagesize: pagesize, count: count, toolbar: false, callback: function (page, size, count) {
				getBabyList(page, size)
			}
		})
		$(".mask, .AddInformation").show()
	})
	$(".AddInformation h4 span").click(function (event) {
		$(".mask, .AddInformation").hide()
	})
	var account = eval('(' + sessionStorage.account + ')')
	var accountName = account.alias
	$("#userName").html(accountName)//检查医生名字
	$("#nowTime").html($common.getNowFormatDate())
	function getAllPostion () {
		$bb.ajax({
			url: URL + "/patient/evaluation/getAllPostion",
			async: false,
			data: JSON.stringify({
				data: {
					"examinationPaperName": type,//评测类型；
					"userId": userId
				}
			}),
			success: function (data) {
				if (data.success) {
					var content = ""
					var num = 1
					$.each(data.data, function (id, value) {
						if (id != "success") {
							content += '<li>' +
								'<a>' + num + "." + value + '</a><br/>' +
								'<label><input type="radio" name="' + id + '" value="1" />几乎从不</label>' +
								'<label><input type="radio" name="' + id + '" value="2" />极少</label> ' +
								'<label><input type="radio" name="' + id + '" value="3" />不常见</label>' +
								'<label><input type="radio" name="' + id + '" value="4" checked="checked" />常见</label>' +
								'<label><input type="radio" name="' + id + '" value="5" />很常见</label>' +
								'<label><input type="radio" name="' + id + '" value="6" />几乎总是</label>' +
								'</li>'
							num++
						}
					})
					$("#postion ul").html(content)
				}
			}
		})
	}

	function getBabyInfo () {
		$bb.ajax({
			url: URL + "/patient/patientInfoPtId",
			data: JSON.stringify({
				data: {
					"patientId": userId
				}
			}),
			success: function (data) {
				var babyInfo = data.data.PatientInfo
				if (data.success) {
					var babyData = $common.babyInfoDeal(babyInfo)
					$common.infoInit({ "id": "#babyInfoList", "data": babyData })
					btnSubmit.removeAttr("disabled")

					//提交测试
					btnSubmit.click(function () {
						var isStart = false
						if ($("#postion ul").find("[name='0']").length > 0) {
							isStart = true
						}
						HashMap.prototype.constructor = HashMap
						var hashMap = new HashMap()
						$("#postion ul :checked").each(function () {
							var id = $(this).attr("name")
							if (isStart) id = id * 1.0 + 1
							hashMap.put(id, $(this).val())
						})
						$bb.ajax({
							url: URL + "/patient/evaluation/getScore",
							data: JSON.stringify({
								data: {
									"userId": userId,
									"examinationPaperName": type,
									"postionIds": JSON.stringify(hashMap)
								}
							}),
							success: function (data) {
								if (data.success) {
									localStorage.setItem("temperament", JSON.stringify(data.data))
									$bb.openPage("temperamentResult", obj)
								}
							}
						})
					})
				} else {
					$bb.toastShow("患者信息获取失败！", 1500)
					btnSubmit.click(function () {
						$bb.toastShow("患者信息不全,无法提交测试！", 1500)
						$(this).attr("disabled", "true")
					})
				}
			}
		})
	}
	//历史记录列表点击：
	$("#dataList").on('click', "ul[result]", function () {
		var data = eval("(" + hashMap.get($(this).attr("result")).replace(/\=/g, "\:") + ")")
		localStorage.setItem("temperament", JSON.stringify(data))
		$bb.openPage("temperamentResult", obj)
	})
	//历史记录列表：
	function getBabyList (page, rows) {
		$bb.ajax({
			url: URL + "/patient/evaluation/getHistoryScore",//baby地址
			async: false,
			data: JSON.stringify({
				data: {
					"userId": userId,
					"examinationPaperName": type,
					"page": page,
					"rows": rows
				}
			}),
			success: function (data) {
				if (data.success) {
					hashMap = new HashMap()
					count = data.total
					var babyData = data.rows
					if (count == 0) {
						$bb.toastShow("患者还未参与该项测评！")
					}
					var str = ''
					for (var i = 0; i < data.rows.length; i++) {
						hashMap.put(data.rows[i].answerId, data.rows[i].result)
						var pretermBirth = 37 * 7 > data.rows[i].pregnancyWeek ? "是" : "否"
						var nowDate = $common.getNowFormatDate()
						var monthAge = $common.DateDiff(nowDate, data.rows[i].birthday) / 30
						var weight = data.rows[i].birthWeight == undefined ? "未知" : data.rows[i].birthWeight + "(g)"
						var doctorName = data.rows[i].doctorName == undefined ? "未知" : data.rows[i].doctorName

						str += '<ul class="clearfix" result="' + data.rows[i].answerId + '">'
						str += '<li>' + data.rows[i].babyName + '</li>'
						str += '<li>' + weight + '</li>'
						str += '<li>' + data.rows[i].birthday + '</li>'
						str += '<li>' + data.rows[i].pregnancyWeek + '(天)</li>'
						str += '<li>' + pretermBirth + '</li>'
						str += '<li>' + doctorName + '</li>'
						str += '<li>' + data.rows[i].createTime + '</li>'
						str += '</ul>'
					}
					$("#dataList").html(str)
				}
			}
		})
	}

})